package com.arron.algorithm.huawei.中等;

import org.omg.CORBA.MARSHAL;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class 在字符串中找出连续最长的数字串 {

    //    abcd12345ed125ss123058789
//            a8a72a6a5yy98y65ee1r2
    public static void main(String[] args) {
        在字符串中找出连续最长的数字串 kk = new 在字符串中找出连续最长的数字串();

        String ss = "a8a72a6a5yy98y65ee1r2";
        kk.findLongestNums(ss);
    }

    public void findLongestNums(String s) {

        String maxStr = "";
        int maxLen = 0;
        int count = 0;
        List<String> list = new ArrayList<>();
        for (int i = 0; i < s.length(); i++) {

            if (Character.isDigit(s.charAt(i))) {
                count++;
                if (count >= maxLen) {
                    maxStr = s.substring(i - count + 1, i + 1);
                    list.add(maxStr);
                }

                maxLen = Math.max(maxLen, count);

            } else {
                count = 0;
            }

        }
        int finalMaxLen = maxLen;
        String s2 = list.stream().filter(s1 -> s1.length() == finalMaxLen).collect(Collectors.joining());
        System.out.println(s2 + "," + maxLen);

    }

    public void findLongestNums_Plus(String str) {
        String input = str;
        String newstring = "";
        char[] charArray = input.toCharArray();
        for (char c : charArray) {
            if (c >= '0' && c <= '9') {
                newstring += c;
            } else {
                newstring += " ";
            }
        }
        String[] serial = newstring.split(" ");
        int max = 0;
        for (String s : serial) {
            max = max > s.length() ? max : s.length();
        }
        for (String s : serial) {
            if (s.length() == max) {
                System.out.print(s);
            }
        }
        System.out.println("," + max);
    }


}
